<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
     <button type="button" class="btn btn-primary edit-btn">
    编辑姓名
    </button>

     <!-- 弹框标签 -->
  <div class="modal name-box" tabindex="-1">
    <div class="modal-dialog">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title">请输入姓名</h5>
          <button type="button" class="btn-close"></button>
        </div>
        <div class="modal-body">
          <form action="">
            <span>姓名：</span>
            <input type="text" class="username">
          </form>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-secondary">取消</button>
          <button type="button" class="btn btn-primary save-btn">保存</button>
        </div>
      </div>
    </div>
  </div>

    <!-- 引入bootstrap.js -->
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.2/dist/js/bootstrap.min.js"></script>
  <script>
      //创建弹框对象
      const modalDom = document.querySelector('.name-box')
      const modal = new bootstrap.Modal(modalDom)

      document.querySelector('.edit-btn').addEventListener
      ('click', ()=>{

        document.querySelector('.username').value = '默认姓名'

        modal.show()
      })



       document.querySelector('.save-btn').addEventListener
      ('click', ()=>{
        const username = document.querySelector('.username').value
        console.log('模拟把姓名保存到服务器', username);
        
        modal.hide()
      })
  </script>
</body>
</html>